#pragma once

#include <QObject>
#include <QNetworkReply>

class QNetworkAccessManager;
namespace AliOSS
{
	class AliOSSAdapter;

	/*!
	* @brief Copyer for AliOSS
	* AliOSSCopyer has similar signals as QNetworkReply for users to \
	* retrive the status of uploading.
	*
	* usage :
	* @code
	* AliOSSAdapter adapter(...);
	* AliOSSCopyer *uploader = adapter->prepareCopy(data);
	* uploader->setNeededInformation(path, signature);
	* uploader->upload();
	* @endcode
	*
	**/
	class AliOSSCopyRequest : public QObject
	{
		Q_OBJECT
	private:
		AliOSSCopyRequest(
			AliOSSAdapter *parent, 
			QNetworkAccessManager *networkAccessManager,
			const QString &bucket,
			const QString &accessId);
	public:
		void doCopy();
		void setNeededInformation(
			const QString &datetimestr,
			const QString &signature);
        inline QMap<QString, QByteArray> ossHeaders()
        {
            return this->_ossHeaders;
        }
        
	public:
		// getter
		QString datetimeString() { return dtstr; }
	public:
		Q_SIGNAL void uploadFinished();
		Q_SIGNAL void uploadFailed();
		Q_SIGNAL void uploadError(QNetworkReply::NetworkError code);
		Q_SIGNAL void uploadProgress(qint64 bytesSent, qint64 bytesTotal);
	protected:
		void onRequestFinished();
	private:
		void prepare(const QString &srcPath, const QString &dstPath);
	private:
		bool isOk();
	private:
		friend class AliOSSAdapter;
		QNetworkAccessManager *networkAccessManager = nullptr;
		QString bucket;
		QString accessId;

        QString srcPath;
		QString dstPath;
		QString dtstr;
		QString signature;
		QMap<QString, QByteArray> _ossHeaders;
		QNetworkReply *reply = nullptr;
	};
}